package com.wissen.temperatureapp.client.widgets;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.wissen.temperatureapp.client.controller.TempController;
import com.wissen.temperatureapp.client.observers.TemperatureObserver;

/**
 * Class represents a Fahrenheit widget. Provides Fahrenheit UI for user.
 * 
 * @author Mayur Birari
 * 
 */
public class FahrenheitWidget extends Composite implements TemperatureObserver {

    private HorizontalPanel fahrenheitPanel;

    private TextBox         fahrenheitTextBox;

    private FlexTable       fahrenheitTable;

    public FahrenheitWidget(){}
    public FahrenheitWidget(Double value) {
        init();

        fahrenheitPanel.add(fahrenheitTable);
        fahrenheitTextBox.setText(value.toString());
        initWidget(fahrenheitPanel);
        TempController.getInstance().addTempObserver(this);
    }

    private void init() {
    	fahrenheitPanel = new HorizontalPanel();
    	fahrenheitTextBox = new TextBox();        

        //creating table
    	fahrenheitTable = new FlexTable();
    	fahrenheitTable.setText(1, 0, " Fahrenheit Value: ");
    	fahrenheitTable.setWidget(1, 1, fahrenheitTextBox);
    	fahrenheitTable.getFlexCellFormatter().setColSpan(2, 0, 2);
    	fahrenheitTable.getFlexCellFormatter().setAlignment(3, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
    }
    
    public void notifyConvertFailed(String errorMessage) {
        Window.alert("Conversion fail: " + errorMessage);
    }
    
    public void notifyConvertSucceeded(Double fvalue) {
    	fahrenheitTextBox.setText(fvalue.toString());
    }
}
